#ifdef USETTS
//! TextToSpeech class definition

#include <QDebug>

#include "include/DownloadManager.h"
#include "include/TextToSpeech.h"
#include "include/Common.h"

#if defined(_WIN32)
#define PHONON
#endif

#ifdef PHONON

#include <Phonon/MediaObject>

#endif

#include <iostream>
#include <QSound>
#include <assert.h>

TextToSpeech::TextToSpeech(QObject* parent):
	m_languageStrings(eLast),
	m_downloadManager( new DownloadManager(parent))
{
	m_languageStrings[eHindi] = "hi";
	m_languageStrings[eEnglish] = "en";
}

TextToSpeech::~TextToSpeech()
{
}

bool TextToSpeech::speak(QString str, Language lang, QUuid uuid, QString deckpath, bool play)
{
	QString genPath = getTFCGeneratedAudioDir(deckpath);
	QString suuid = uuid.toString();

	QString filename = genPath + "/" + suuid + ".mp3";

	QFile file(filename);

	if (file.exists() == true && file.size() > 1024)
	{
		if (play)
		{
#ifdef PHONON
			static Phonon::MediaObject *music =
			    Phonon::createPlayer(Phonon::MusicCategory,
			                         Phonon::MediaSource(filename));
			music->setCurrentSource(filename);
			music->play();
#else
			QString play = "play ";
			if (lang == eHindi)
				play += " --volume 3";
			play += " \"";
			play += filename;
			play += "\" 2>/dev/null &";
			system(play.toStdString().c_str());
#endif
		}
		return false;
	}
	else
	{
		QString surl = "http://translate.google.com/translate_tts?";
		surl += "tl=";
		surl += m_languageStrings[lang];
		surl += "&q=";
		surl += str;

		QUrl url(surl);
		qDebug() << url << endl;

		DownloadRequest request;
		request.m_networkReply = 0;
		request.m_filename = filename;
		request.m_url = url;

		m_downloadManager->doDownload(request);
		return true;
	}
}
#endif
